//
//  XRCommonPopSheetView.m
//  XClassRoom
//
//  Created by 余伟 on 2016/12/26.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "XRCommonPopSheetView.h"
#import <objc/runtime.h>
static  const  void *XRCommonPopSheetViewkey= &XRCommonPopSheetViewkey;
@interface XRCommonPoptextCell :UITableViewCell
@property(nonatomic,retain)UILabel  *textlable;
@end

@implementation XRCommonPoptextCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _textlable=[[UILabel alloc] init];
        _textlable.textAlignment=NSTextAlignmentCenter;
        _textlable.text=@" ";
        _textlable.textColor=[UIColor blackColor];
        [self.contentView addSubview:_textlable];
        [_textlable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(self);
        }];
    }
    return self;
}
@end

@interface XRCommonPopSheetView ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView      *_tabeleView;
    UIButton         *_popCover;
    NSMutableArray   *_dataListArray;
    ClickBlock        _block;
}
@end
@implementation XRCommonPopSheetView
-(instancetype)initWithTitleArray:(NSArray *)titleArray cancelButtonTitle:(NSString *) cancelButtonTitlel
{
    if (self=[super init])
    {
        _dataListArray=[[NSMutableArray alloc] init];
        [_dataListArray addObjectsFromArray:titleArray];
        [_dataListArray addObject:cancelButtonTitlel];
        _tabeleView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 100) style:UITableViewStylePlain];
        _tabeleView.delegate=self;
        _tabeleView.dataSource=self;
        _tabeleView.tableFooterView=[[UIView alloc] init];
        CGFloat tableHeight=(titleArray.count+1)*46+10>kMainScreenHeight?kMainScreenHeight:(titleArray.count+1)*46+10;
        _tabeleView.frame=CGRectMake(0, kMainScreenHeight-tableHeight, kMainScreenWidth, tableHeight);
        [_tabeleView reloadData];
    }
    return self;
}
-(instancetype)initWithTitleArray:(NSArray *)titleArray cancelButtonTitle:(NSString *) cancelButtonTitlel ClickBlock:(ClickBlock)clickBlock
{
    if (self=[super init])
    {
        _block=clickBlock;
        _dataListArray=[[NSMutableArray alloc] init];
        [_dataListArray addObjectsFromArray:titleArray];
        [_dataListArray addObject:cancelButtonTitlel];
        _tabeleView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 100) style:UITableViewStylePlain];
        _tabeleView.delegate=self;
        _tabeleView.dataSource=self;
        _tabeleView.tableFooterView=[[UIView alloc] init];
        CGFloat tableHeight=(titleArray.count+1)*46+10>kMainScreenHeight?kMainScreenHeight:(titleArray.count+1)*46+10;
        _tabeleView.frame=CGRectMake(0, kMainScreenHeight-tableHeight, kMainScreenWidth, tableHeight);
        [_tabeleView reloadData];
    }
    return self;
    
}
-(void)show
{
    _popCover =[[UIButton alloc] init];
    [_popCover addTarget:self action:@selector(clickPopCover:) forControlEvents:UIControlEventTouchUpInside];
    _popCover.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.6];
    _popCover.frame = [UIScreen mainScreen].bounds;
    [_popCover addSubview:_tabeleView];
    [self addSubview:_popCover];
    self.frame=_popCover.bounds;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)return _dataListArray.count-1;
    return 1;
}
//cell 定格
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}
#pragma mark UITableViewdelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XRCommonPoptextCell  *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XRCommonPoptextCell class])];
    if (cell==nil)
    {
        cell=[[XRCommonPoptextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XRCommonPoptextCell class])];
    }
    if (indexPath.section==0)
    {
     cell.textlable.text=[NSString stringWithFormat:@"%@",_dataListArray[indexPath.row]];
        cell.textlable.textColor=kBlackColor;
    }else
    {
       cell.textlable.text=@"取消";
        cell.textlable.textColor=kNavBarThemeColor;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView  *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 10)];
    [view setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.6]];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
    
}
#pragma mark 点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self clicktableViewindex:indexPath];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  *view=[[UIView alloc] init];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0;
}
#pragma mark 点击单元
-(void)clicktableViewindex:(NSIndexPath *)indexpath
{
    [self removeFromSuperview];
    NSInteger  indexM=0;
    switch (indexpath.section)
    {
        case 0:
            indexM=indexpath.row+1;
            break;
        case 1:
            indexM=0;
            break;
        default:
            break;
    }
    if (_block) {
        _block(indexM);
    }
    
}
#pragma mark 按钮点击事件
-(void)clickPopCover:(UIButton *)sender
{
    [self removeFromSuperview];
    if (_block) {
        _block(0);
    }
    
    
}
@end
